import os
import platform
import sys

from setuptools import setup, find_packages
from wheel.bdist_wheel import bdist_wheel

from publish import devpi_login, devpi_upload

with open('README.md', 'r', encoding='utf-8') as f:
    long_description = f.read()


class CustomWheelBuilder(bdist_wheel):
    def get_tag(self):
        python, abi, plat = super().get_tag()
        # 自动检测Python版本和平台信息
        python_tag = f'cp{sys.version_info.major}{sys.version_info.minor}'
        abi_tag = f'{python_tag}m' if hasattr(sys, 'gettotalrefcount') else python_tag
        platfrom = "win" if sys.platform == "win32" else sys.platform
        platform_tag = f'{platfrom}_{platform.machine().lower()}'
        # 你可以在这里修改python, abi, 或 plat标签
        return python_tag, abi_tag, platform_tag  # 示例：修改plat标签

    def run(self):
        super().run()
        impl_tag, abi_tag, plat_tag = self.get_tag()
        archive_basename = f"{self.wheel_dist_name}-{impl_tag}-{abi_tag}-{plat_tag}"
        wheel_path = os.path.join(self.dist_dir, archive_basename + ".whl")

        devpi_login()
        devpi_upload([wheel_path])
        print(f"devpi upload 已完成！{wheel_path}")


setup(
    name='pika_charts',
    version='0.1.4',
    packages=find_packages(),
    python_requires='>=3.11',
    install_requires=[
        'pandas',
        'pywebview>=5.0.5',
    ],
    package_data={
        'pika_charts': ['js/*'],
    },
    author='louisnw',
    license='MIT',
    description="Python framework for TradingView's Lightweight Charts JavaScript library.",
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://gitee.com/yexf/pika_charts',
    cmdclass={
        'bdist_wheel': CustomWheelBuilder,  # 使用自定义的wheel构建器
    }
)
